﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TankBattle.Models
{
    public class PortralGate
    {
        #region Variable
        protected List<Sprite> sprites;
        protected List<Vector2> positions;
        protected List<Texture2D> textures;
        protected Random random;
        #endregion 

        #region Construction 
        public PortralGate()
        { }
        public PortralGate(Texture2D texture, Texture2D texture2)
        {
            Initallize(texture, texture2);
        }
        #endregion 

        #region Properties
        public  Rectangle Polygon(int i)
        {
            return new Rectangle(
                    (int)(positions[i].X + Constant.PORTRAL_RADIUS),
                    (int)(positions[i].Y + Constant.PORTRAL_RADIUS),
                    Constant.PORTRAL_WIDTH -  Constant.PORTRAL_RADIUS * 2,
                    Constant.PORTRAL_HEIGHT - Constant.PORTRAL_RADIUS * 2
                    );
        }
        #endregion

        #region Main Function 
        protected  void Initallize(Texture2D texture, Texture2D texture2)
        {
            sprites = new List<Sprite>();
            positions = new List<Vector2>();
            textures = new List<Texture2D>();
            random = new Random(DateTime.Now.Millisecond);

            textures.Add(texture);
            textures.Add(texture2);
        }
        public  bool Update(List<GameObject> objects)
        {
            foreach (Sprite sprite in sprites)
            {
                sprite.Update();
            }
            Action(objects);
            return true;
        }
        public  void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < sprites.Count; i++)
            {
                sprites[i].Draw(spriteBatch, positions[i]);
            }
        }
        #endregion 

        #region Other Function 
        protected virtual void Action(List<GameObject> objects)
        {
            for (int i = 0; i < positions.Count;i++)
            foreach (GameObject obj in objects)
            {
                if (Polygon(i).Intersects(obj.Polygon) && !obj.UnDead)
                {    
                    obj.SetPosition(positions[random.Next(positions.Count)]);
                    obj.SetUnDead(true);
                }
            }
        }
        public virtual void Add(Vector2 position)
        {
            positions.Add(position);

            sprites.Add(
                new Sprite(textures[random.Next(textures.Count)], 
                    new Vector2(Constant.PORTRAL_SIZE_X, Constant.PORTRAL_SIZE_Y),
                    new Vector2(Constant.PORTRAL_SHEET_X, Constant.PORTRAL_SHEET_Y),
                    Constant.RATE_PORTRAL,
                    true
                    ));
        }
        #endregion 
    }
}
